from coffeetalk.protocol import ioelement

class Subscriber(object):
    
    def __init__(self,name=None,username='',group='',handler=None,**kwargs):
        self.name = name
        self.username = username
        self.group = group
        self.handler = handler
    
    def __str__(self):
        return str(self._to_element())
    def _to_element(self):
        return ioelement.IOElement(self.name,{'type':'[Subscriber]','username':self.username,'group':self.group,'handler':str(self.handler)})

class OldSubscriber:
#TODO: Fix this class to be in accordance with all the other builtins  
    def __init__(self,username,group,connectionid,handler):
        self.username = username
        self.group = group
        self.connectionid = connectionid
        self.handler = handler
        
    def to_ioelement(self):
        return ioelement.IOElement(self.username,{'type':'[Subscriber]','group':self.group,'connectionid':self.connectionid,'handler':str(self.handler)})
    
    def from_ioelement(self,element):
        self.username = element.name
        self.group = element.attribs['group']
        self.connectionid = element.attribs['connectionid']
        self.handler = element.attribs['handler']
